package com.cndw.kungfu.dao;

import java.util.List;

import com.cndw.dao.jade.Identity;
import com.cndw.dao.jade.annotation.DAO;
import com.cndw.dao.jade.annotation.SQL;
import com.cndw.dao.jade.annotation.SQLParam;

import com.cndw.kungfu.domain.Mail;

@DAO
public interface MailDao {

	public static final String TABLE = " kf_player_mail ";

	public static final String FIELDS = " id,playerId,playerName,senderId,senderName,title,content,addTime,type,saved,readed,status,senderDel,attachment,getTime";

	public static final String NoContentFIELDS = " id,playerId,playerName,senderId,senderName,title,addTime,type,saved,readed,status,senderDel";

	public static final int delStatus = 1;

	@SQL("select "
			+ NoContentFIELDS
			+ " from "
			+ TABLE
			+ " where #if(:action==2) {senderId=:pid and senderDel=0} #else {playerId=:pid and status=0}  #if(:action==3){ and saved=1}#else{ and saved =0}  #if(:type>0){ and type=:type } order by id desc limit :offset,:limit ")
	public List<Mail> select(@SQLParam("pid") long playerId, @SQLParam("type") int type, @SQLParam("action") int action, @SQLParam("offset") int offset,
			@SQLParam("limit") int limit);

	@SQL("select Count(*) from " + TABLE
			+ " where #if(:action==2) {senderId=:pid and senderDel=0} #else {playerId=:pid and status=0}  #if(:action==3){ and saved=1}#else{ and saved =0}  #if(:type>0){ and type=:type }  ")
	public int getCount(@SQLParam("pid") long playerId, @SQLParam("type") int type, @SQLParam("action") int action);

	@SQL("INSERT INTO " + TABLE + " (" + FIELDS
			+ ") VALUES(:m.id,:m.playerId,:m.playerName,:m.senderId,:m.senderName,:m.title,:m.content,:m.addTime,:m.type,:m.saved,:m.readed,:m.status,:m.senderDel,:m.attachment,:m.getTime) ")
	public Identity insert(@SQLParam("m") Mail mail);

	@SQL("select " + FIELDS + " from " + TABLE + " where playerId=:1 and id=:2  ")
	public Mail getReceivedMail(long playerId, int id);

	@SQL("select " + FIELDS + " from " + TABLE + " where senderId=:1 and id=:2  ")
	public Mail getSendedMail(long playerId, int id);

	@SQL("update " + TABLE + " set readed = 1 where playerId =:playerId and id in (:ids) ")
	public void signReaded(@SQLParam("playerId") long playerId, @SQLParam("ids") int[] ids);

	@SQL("update " + TABLE + " set readed = 1 where playerId =:playerId and id = :id ")
	public void signReaded(@SQLParam("playerId") long playerId, @SQLParam("id") int id);

	@SQL("update " + TABLE + " set saved = 1 where playerId =:playerId and id =:id ")
	public void signSaved(@SQLParam("playerId") long playerId, @SQLParam("id") int id);

	@SQL("update " + TABLE + " set status =" + delStatus + " where playerId =:playerId and id in (:ids)  ")
	public void signDel(@SQLParam("playerId") long playerId, @SQLParam("ids") int[] ids);

	@SQL("update " + TABLE + " set senderDel =" + delStatus + " where senderId =:playerId and id in (:ids) ")
	public void signSendDel(@SQLParam("playerId") long playerId, @SQLParam("ids") int[] ids);

	@SQL("update " + TABLE + " set status =" + delStatus + " where playerId =:playerId #if(:type>0){ and type=:type }  and saved=:saved and (attachment is null or getTime > 0 )")
	public void delAll(@SQLParam("playerId") long playerId, @SQLParam("type") int type, @SQLParam("saved") int saved);

	@SQL("update " + TABLE + " set senderDel =" + delStatus + " where senderId =:playerId #if(:type>0){ and type=:type }")
	public void delSendAll(@SQLParam("playerId") long playerId, @SQLParam("type") int type);

	@SQL("update " + TABLE + " set getTime = :getTime where playerId =:playerId and id =:id ")
	public void receiveAttachment(@SQLParam("playerId") long playerId, @SQLParam("id") int id, @SQLParam("getTime") int getTime);

	@SQL("delete from "+TABLE+" where readed=0 and saved=0 and addTime<=UNIX_TIMESTAMP()-20*86400 limit 200 ")
	public void clearData20();
	
	@SQL("delete from "+TABLE+" where readed=1 and saved=0 and addTime<=UNIX_TIMESTAMP()-5*86400 limit 200 ")
	public void clearData5();

	@SQL("select "
			+ FIELDS
			+ " from "
			+ TABLE
			+ " where #if(:action==2) {senderId=:pid } #else {playerId=:pid }  #if(:action==3){ and saved=1}#else{ and saved =0} #if(:haveAtt) {and attachment != ''} #if(:gotAtt) {and getTime >0 }  order by id desc limit :offset,:limit ")	
	public List<Mail> getMailsGM(
			@SQLParam("pid") long pid,
			@SQLParam("action") int action, 
			@SQLParam("haveAtt") boolean haveAtt, 
			@SQLParam("gotAtt") boolean gotAtt,
			@SQLParam("offset")int offset, 
			@SQLParam("limit")int limit);
	
	@SQL("select count(*) from "+ TABLE + " where #if(:action==2) {senderId=:pid } #else {playerId=:pid }  #if(:action==3){ and saved=1}#else{ and saved =0} #if(:haveAtt) {and attachment != ''} #if(:gotAtt) {and getTime >0 } ")
	public int getMailsGMCout(@SQLParam("pid") long pid,
			@SQLParam("action") int action, 
			@SQLParam("haveAtt") boolean haveAtt, 
			@SQLParam("gotAtt") boolean gotAtt);

}
